Last updated: 2023-07-31

Checks: 5 1

Knit directory: WenjunLiu_Thesis_Chapter4/

This reproducible R Markdown analysis was created with workflowr (version 1.7.0). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20200930) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Tracking code development and connecting the code version to the results is critical for reproducibility. To start using Git, open the Terminal and type git init in your project directory.


This project is not being versioned with Git. To obtain the full reproducibility benefits of using workflowr, please see ?wflow_start.


library(ngsReports)
library(tidyverse)
library(yaml)
library(scales)
library(pander)
library(glue)
library(cowplot)
library(plotly)
panderOptions("table.split.table", Inf)
panderOptions("big.mark", ",")
theme_set(theme_bw())
config <- here::here("config/config.yml") %>%
  read_yaml()
suffix <- paste0(config$tag, config$ext)
sp <- config$ref$species %>%
  str_replace("(^[a-z])[a-z]*_([a-z]+)", "\\1\\2") %>%
  str_to_title()
samples <- config$samples %>%
  here::here() %>%
  read_tsv() %>%
  mutate(
    Filename = paste0(sample, suffix)
  ) %>%
  mutate_if(
    function(x){length(unique(x)) < length(x)},
    as.factor
  )
config$analysis <- config$analysis %>%
  lapply(intersect, y = colnames(samples)) %>%
  .[vapply(., length, integer(1)) > 0]
if (length(config$analysis)) {
  samples <- samples %>%
    unite(
      col = group, 
      any_of(as.character(unlist(config$analysis))), 
      sep = "_", remove = FALSE
    ) 
} else {
  samples$group <- samples$Filename
}
group_cols <- hcl.colors(
  n = length(unique(samples$group)), 
  palette = "Zissou 1"
  ) %>%
  setNames(unique(samples$group))

Quality Assessment on Trimmed Data

In the workflow, trimming was performed using the tool AdapterRemoval with the settings:

  • Adapter Sequence: AGATCGGAAGAGCACACGTCTGAACTCCAGTCA
  • Minimum length after trimming: 35
  • Minimum quality score to retain: 30
  • Maximum allowable number of N bases to allow: 1

Overall Summary

rawFqc <- here::here("data/raw/FastQC") %>%
  list.files(pattern = "zip", full.names = TRUE) %>%
  FastqcDataList() %>%
  .[fqName(.) %in% samples$Filename]
trimFqc <- here::here("data/trimmed/FastQC") %>%
  list.files(pattern = "zip", full.names = TRUE) %>%
  FastqcDataList() %>%
  .[fqName(.) %in% samples$Filename]

After trimming, the library showing the highest level of possible adapter content contained 0.15% of reads as containing possible adapter sequences.

a <- plotSummary(rawFqc, pattern = suffix) +
  theme(axis.text.y=element_blank()) 
b <- plotSummary(trimFqc, pattern = suffix) +
  theme(
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    axis.title.y = element_blank()
  )
plot_grid(
  a + theme(legend.position = "none"),
  b + theme(legend.position = "none"),
  labels = c("A", "B"),
  nrow = 1,
  rel_widths = c(1.6, 1)
) +
  draw_plot(
    plot = get_legend(a), 
    x = -0.3, 
    y = -0.4, 
  )
*Comparison of FastQC summaries A) before and B), after trimming*

Comparison of FastQC summaries A) before and B), after trimming

Library Sizes

readTotals(rawFqc) %>%
  rename(Raw = Total_Sequences) %>%
  left_join(
    readTotals(trimFqc) %>%
      rename(Trimmed = Total_Sequences)
  ) %>%
  mutate(
    Remaining = Trimmed / Raw,
    Filename = str_remove_all(Filename, suffix)
  ) %>%
  summarise(
    across(c(Remaining, Trimmed), list(min = min, mean = mean, max = max))
  ) %>%
  pivot_longer(
    everything()
  ) %>%
  separate(
    name, into = c("Type", "Summary Statistic")
  ) %>%
  pivot_wider(names_from = Type, values_from = value) %>%
  mutate(
    Remaining = percent(Remaining, accuracy = 0.1),
    `Summary Statistic` = str_to_title(`Summary Statistic`)
  ) %>%
  rename(Reads = Trimmed) %>%
  pander(
    caption = "*Summary statistics showing the results after trimming*"
  )
Summary statistics showing the results after trimming
Summary Statistic Remaining Reads
Min 96.2% 947,500
Mean 97.0% 2,167,361
Max 97.7% 5,995,508

Sequence Length Distribution

ggplotly(
  getModule(trimFqc, "Sequence_Length") %>%
    group_by(Filename) %>%
    mutate(
      `Cumulative Total` = cumsum(Count),
      `Cumulative Percent` = percent(`Cumulative Total` / max(`Cumulative Total`))
    ) %>%
    ungroup() %>%
    left_join(samples) %>%
    rename_all(str_to_title) %>%
    ggplot(aes(Length, `Cumulative Total`, group = Filename, label = `Cumulative Percent`)) +
    geom_line(aes(colour = Group), size = 1/4) +
    scale_y_continuous(label = comma) +
    scale_colour_manual(
      values = group_cols
    ) 
)

Distribution of read lengths after trimming

GC Content

ggplotly(
  getModule(trimFqc, "Per_sequence_GC_content") %>%
    group_by(Filename) %>%
    mutate(
      cumulative = cumsum(Count) / sum(Count)
    ) %>%
    ungroup() %>%
    left_join(samples) %>%
    bind_rows(
      getGC(gcTheoretical, sp, "Trans") %>%
        mutate_at(sp, cumsum) %>% 
        rename_all(
          str_replace_all, 
          pattern = sp, replacement = "cumulative",
        ) %>%
        mutate(
          Filename = "Theoretical GC",
          group = Filename
        )
    ) %>%
    mutate(
      group = as.factor(group),
      group = relevel(group, ref = "Theoretical GC"),
      cumulative = round(cumulative*100, 2)
    ) %>%
    ggplot(aes(GC_Content, cumulative, group = Filename)) +
    geom_line(aes(colour = group), size = 1/4) +
    scale_x_continuous(label = ngsReports:::.addPercent) +
    scale_y_continuous(label = ngsReports:::.addPercent) +
    scale_colour_manual(
      values = c("#000000", group_cols)
    ) +
    labs(
      x = "GC Content",
      y = "Cumulative Total",
      colour = "Group"
    )
)

GC content shown as a cumulative distribution for all libraries. Groups can be hidden by clicking on them in the legend.

Sequence Content

plotly::ggplotly(
  getModule(trimFqc, module = "Per_base_sequence_content") %>% 
    mutate(Base = fct_inorder(Base)) %>%
    group_by(Base) %>% 
    mutate(
      across(c("A", "C", "G", "T"), function(x){x - mean(x)}) 
    ) %>% 
    pivot_longer(
      cols = c("A", "C", "G", "T"), 
      names_to = "Nuc", 
      values_to = "resid"
    ) %>%
    left_join(samples) %>%
    ggplot(
      aes(Base, resid, group = Filename, colour = group)
    ) + 
    geom_line() +
    facet_wrap(~Nuc) + 
    scale_colour_manual(values = group_cols) +
    labs(
      x = "Read Position", y = "Residual", colour = "Group"
    )
)

Base and Position specific residuals for each sample. The mean base content at each position was calculated for each nucleotide, and the sample-specific residuals calculated.


sessionInfo()
R version 4.3.0 (2023-04-21)
Platform: x86_64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.3.1

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRblas.0.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: Australia/Adelaide
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] plotly_4.10.2       cowplot_1.1.1       glue_1.6.2         
 [4] pander_0.6.5        scales_1.2.1        yaml_2.3.7         
 [7] lubridate_1.9.2     forcats_1.0.0       stringr_1.5.0      
[10] dplyr_1.1.2         purrr_1.0.1         readr_2.1.4        
[13] tidyr_1.3.0         tidyverse_2.0.0     ngsReports_2.2.4   
[16] tibble_3.2.1        patchwork_1.1.2     ggplot2_3.4.2      
[19] BiocGenerics_0.46.0

loaded via a namespace (and not attached):
 [1] tidyselect_1.2.0        viridisLite_0.4.2       farver_2.1.1           
 [4] Biostrings_2.68.1       bitops_1.0-7            fastmap_1.1.1          
 [7] lazyeval_0.2.2          RCurl_1.98-1.12         promises_1.2.0.1       
[10] digest_0.6.33           timechange_0.2.0        lifecycle_1.0.3        
[13] ellipsis_0.3.2          magrittr_2.0.3          compiler_4.3.0         
[16] rlang_1.1.1             sass_0.4.6              tools_4.3.0            
[19] utf8_1.2.3              data.table_1.14.8       knitr_1.43             
[22] labeling_0.4.2          htmlwidgets_1.6.2       bit_4.0.5              
[25] here_1.0.1              workflowr_1.7.0         withr_2.5.0            
[28] grid_4.3.0              stats4_4.3.0            fansi_1.0.4            
[31] git2r_0.32.0            colorspace_2.1-0        MASS_7.3-60            
[34] cli_3.6.1               rmarkdown_2.23          crayon_1.5.2           
[37] generics_0.1.3          rstudioapi_0.15.0       httr_1.4.6             
[40] tzdb_0.4.0              cachem_1.0.8            zlibbioc_1.46.0        
[43] parallel_4.3.0          XVector_0.40.0          vctrs_0.6.3            
[46] jsonlite_1.8.7          IRanges_2.34.1          hms_1.1.3              
[49] S4Vectors_0.38.1        bit64_4.0.5             crosstalk_1.2.0        
[52] jquerylib_0.1.4         ggdendro_0.1.23         DT_0.28                
[55] stringi_1.7.12          gtable_0.3.3            later_1.3.1            
[58] GenomeInfoDb_1.36.1     munsell_0.5.0           pillar_1.9.0           
[61] htmltools_0.5.5         GenomeInfoDbData_1.2.10 R6_2.5.1               
[64] rprojroot_2.0.3         vroom_1.6.3             evaluate_0.21          
[67] lattice_0.21-8          highr_0.10              httpuv_1.6.11          
[70] bslib_0.5.0             Rcpp_1.0.11             xfun_0.39              
[73] fs_1.6.2                zoo_1.8-12              pkgconfig_2.0.3